package org.southyin.dao;

import org.southyin.model.CmsCustomer;
import org.southyin.utils.DBUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class CmsCustomerDaoImpl implements CmsCustomerDao {
    public int findCustomerByEmail(String email) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        int count = 0;
        try {
            conn = DBUtils.getInstance().getConnection();
            String sql = "select count(id) as counts from cms_customer where email = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1, email);
            rs = ps.executeQuery();
            if(rs.next()){
                count = rs.getInt("counts");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtils.getInstance().close(rs);
            DBUtils.getInstance().close(ps);
            DBUtils.getInstance().close(conn);
        }
        return count;
    }

    public int saveCustomer(CmsCustomer cmsCustomer) {
        Connection conn = null;
        PreparedStatement ps = null;
        int count = 0;
        try {
            conn = DBUtils.getInstance().getConnection();
            conn.setAutoCommit(false);
            String sql = "insert into cms_customer(email,password,nickname) values(?,?,?)";
            ps = conn.prepareStatement(sql);
            ps.setString(1,cmsCustomer.getEmail());
            ps.setString(2,cmsCustomer.getPassword());
            ps.setString(3,"新用户");
            count = ps.executeUpdate();
            conn.commit();
        } catch (Exception e) {
            if (conn != null) {
                try {
                    conn.rollback();
                } catch (SQLException ex) {
                    ex.printStackTrace();
                }
            }
            e.printStackTrace();
        } finally {
            DBUtils.getInstance().close(ps);
            DBUtils.getInstance().close(conn);
        }
        return count;
    }

    public CmsCustomer findCustomerByEmailAndPass(CmsCustomer cmsCustomer) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        CmsCustomer customer = null;
        try {
            conn = DBUtils.getInstance().getConnection();
            String sql = "select * from cms_customer where email = ? and password = ?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,cmsCustomer.getEmail());
            ps.setString(2, cmsCustomer.getPassword());
            rs = ps.executeQuery();
            if (rs.next()) {
                customer = new CmsCustomer();
                customer.setId(rs.getLong("id"));
                customer.setEmail(rs.getString("email"));
                customer.setPassword(rs.getString("password"));
                customer.setNickname(rs.getString("nickname"));
                customer.setGender(rs.getBoolean("gender"));
                customer.setLocation(rs.getString("location"));
                customer.setSign(rs.getString("sign"));
                customer.setJoinTime(rs.getTimestamp("joinTime"));
                customer.setEnabled(rs.getBoolean("enabled"));
                customer.setDelMark(rs.getBoolean("delMark"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtils.getInstance().close(rs);
            DBUtils.getInstance().close(ps);
            DBUtils.getInstance().close(conn);
        }
        return customer;
    }

    public CmsCustomer getPersonInfos(Long id) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        CmsCustomer customer = null;
        try {
            conn = DBUtils.getInstance().getConnection();
            String sql = "select * from cms_customer where id = ?";
            ps = conn.prepareStatement(sql);
            ps.setLong(1,id);
            rs = ps.executeQuery();
            if (rs.next()) {
                customer = new CmsCustomer();
                customer.setId(rs.getLong("id"));
                customer.setEmail(rs.getString("email"));
                customer.setNickname(rs.getString("nickname"));
                customer.setGender(rs.getBoolean("gender"));
                customer.setLocation(rs.getString("location"));
                customer.setSign(rs.getString("sign"));
                customer.setJoinTime(rs.getTimestamp("joinTime"));
                customer.setEnabled(rs.getBoolean("enabled"));
                customer.setDelMark(rs.getBoolean("delMark"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtils.getInstance().close(rs);
            DBUtils.getInstance().close(ps);
            DBUtils.getInstance().close(conn);
        }
        return customer;
    }
}
